<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="770"
      :before-close="handleClose"
      :show-close="false"
    >
      <div>
        <div class="title">
          <span>选择机构负责人</span>
        </div>
        <div class="selectedRole">
          <div class="top">
            <span>已选机构负责人</span>
          </div>
          <div class="bottom">
            <span v-if="selectedList.length < 1">暂未选择</span>
            <template v-else>
              <div v-for="item in selectedList" :key="item.id">
                <img :src="item.userUrl" alt="" />
                <p>{{ item.userName }}</p>
              </div>
            </template>
          </div>
        </div>
        <div class="roleList">
          <div class="top">
            <span>人员列表</span>
          </div>
          <div class="search">
            <input
              type="text"
              placeholder="搜索"
              v-model="searchInput"
              @keyup="searchList"
            />
            <i class="el-icon-search icon" @click="searchList"></i>
          </div>
          <div class="list">
            <ul v-if="roleList.length > 0">
              <li v-for="item in roleList" :key="item.id">
                <div>
                  <img :src="item.userUrl" alt="" />
                  <span>{{ item.userName }}</span>
                </div>
                <div>
                  <input
                    type="checkbox"
                    name=""
                    id=""
                    @click="selected(item, $event)"
                    :checked="item.isSelected"
                  />
                </div>
              </li>
              <!-- <li>
                      <div>
                        <img src="../../../assets/addfunction.png" alt="">
                      <span>11111</span>
                      </div>
                      <div>
                          <input type="checkbox" name="" id="">
                      </div>
                  </li> -->
            </ul>
            <span
              v-else
              style="display: block; padding-top: 20px; padding-left: 20px"
              >暂无可选机构负责人</span
            >
          </div>
          <div class="btn">
            <button @click="cancel">取消</button>
            <button @click="sendRoleList">确定</button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentUser } from '../../../api/systemManage/system';
export default {
  created() {
    this.searchInput = '';
  },
  data() {
    return {
      dialogVisible: false,
      roleList: [],
      selectedList: [],
      searchInput: '',
      departmentid: '',
      receivedId: ''
    };
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 打开选人的模态框
    openEdit(val, val2) {
      // console.log('传过来的数据', val2, val);
      this.receivedId = val2;
      //   this.backUser = val2;
      this.departmentid = val;
      this.dialogVisible = true;
      this.getList();
    },
    // 点击取消
    cancel() {
    //   this.selectedList = this.backUser;
    //   console.log(this.selectedList);
      //   this.sendRoleList();
      this.dialogVisible = false;
    },
    // 获取人员列表
    async getList() {
    //   const userId = sessionStorage.getItem('userid');
    //   console.log('userid', userId);
      const res = await getDepartmentUser(this.departmentid);
      // console.log(res);
      this.roleList = res.data;
      this.selectedList = this.receivedId;
      //   console.log(this.selectedList);
      // 给之前选择的赋值
      this.roleList.forEach(item => {
        item.isSelected = false;
        this.receivedId.forEach(ele => {
          if (item.userId === ele.userId) {
            // console.log(item);
            item.isSelected = true;
            // this.selectedList.push(item);
          }
        });
      });
      // console.log(this.roleList);
    },
    // 点击选择人员
    selected(val, e) {
      // console.log(this.selectedList);
      // console.log(e.target.checked);
      if (e.target.checked === true) {
        // console.log(val);
        this.selectedList.push(val);
      } else {
        // console.log(val);
        const newArr = this.selectedList.filter(item => {
          return item.userId !== val.userId;
        });
        // console.log(newArr);
        this.selectedList = newArr;
      }
    },
    // 确认选择的人员
    sendRoleList() {
      this.$emit('getRoleList', this.selectedList);
      this.roleList = [];
      this.dialogVisible = false;
      // console.log('确认选取的人员', this.selectedList);
    },
    // 搜索人员
    async searchList() {
      // console.log(this.searchInput);
      const res = await getDepartmentUser(this.departmentid, this.searchInput);
      // console.log(res);
      this.roleList = res.data;
    }
  },
  beforeDestroy() {
    this.selectedList = [];
  }
};
</script>

<style scoped lang="less">
button {
  cursor: pointer;
}
/deep/.el-dialog__header,
/deep/.el-dialog__body {
  padding: 0;
}
/deep/.el-dialog {
  width: 770px;
  box-shadow: 3px 3px 11px 0px #3d3d3d;
}
.title {
  height: 55px;
  width: 100%;
  background: #f1f5ff;
  text-align: center;
  span {
    font-size: 16px;
    font-weight: 400;
    color: #333333;
    line-height: 55px;
  }
}
.selectedRole {
  height: 150px;
  // background: rebeccapurple;
  padding-top: 10px;
  padding-left: 13px;
  box-sizing: border-box;
  .top {
    span {
      font-size: 14px;
      font-weight: 400;
      color: #333333;
    }
  }
  .bottom {
    margin-top: 5px;
    display: flex;
    div {
      margin: 0px 5px;
      text-align: center;
      p {
        margin: 0;
        padding: 0;
      }
      img {
        height: 57px;
        width: 57px;
        border-radius: 50%;
      }
    }
  }
}
.roleList {
  padding-left: 20px;
  .top {
    span {
      font-size: 14px;
      font-weight: 400;
      color: #333333;
    }
  }
  .search {
    margin-top: 10px;
    position: relative;
    input {
      width: 729px;
      height: 40px;
      background: #ffffff;
      border: 0;
      border: 1px solid #eeeeee;
      border-radius: 24px;
      text-align: center;
    }
    .el-icon-search.icon {
      background: #2fc0e0;
      color: white;
      height: 40px;
      width: 40px;
      border-radius: 50%;
      text-align: center;
      line-height: 40px;
      position: absolute;
      right: 18px;
      top: 2px;
    }
  }
  .list {
    height: 300px;
    width: 98%;
    overflow-y: scroll;
    // background: #2FC0E0;
    ul {
      li {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eeeeee;
        line-height: 60px;
        div {
          box-sizing: border-box;
          &:nth-child(1) {
            display: flex;
            align-items: center;
          }
          img {
            height: 51px;
            width: 51px;
            margin-right: 10px;
          }
        }
      }
    }
  }
  .btn {
    text-align: center;
    margin-top: 26px;
    padding-bottom: 36px;
    button {
      width: 100px;
      height: 35px;
      background: #e8f8fb;
      border-radius: 4px;
      margin: 0px 42px;
      color: #2fc0e0;
      &:nth-child(2) {
        background: #2fc0e0;
        color: white;
      }
    }
  }
}
input {
  outline: none;
}
</style>
